﻿using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public enum State
{
    Horizontal,
    Vertical,
    HV
}
public class PlayerController : MonoBehaviour {

    
    public State state;
    bool outOfControll = false;
	// Use this for initialization
	void Start () {
        state = State.Horizontal;
	}
	
	// Update is called once per frame
	void Update () {
        //左翻
        if (Input.GetKeyDown(KeyCode.LeftArrow)) {


            if (state == State.Horizontal)
            {
                //transform.rotation = Quaternion.AngleAxis(90f, transform.forward)*transform.rotation;
                transform.RotateAround(transform.position - new Vector3(1, 0.5f, 0), Vector3.forward, 90f);
                state = State.Vertical;
            }
            else if (state == State.Vertical)
            {
                transform.RotateAround(transform.position - new Vector3(0.5f, 1, 0), Vector3.forward, 90f);
                state = State.Horizontal;
            }
            else {
                transform.RotateAround(transform.position - new Vector3(0.5f, 0.5f, 0), Vector3.forward, 90f);
            }
            
        }

        //右翻
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {


            if (state == State.Horizontal)
            {
                //transform.rotation = Quaternion.AngleAxis(90f, transform.forward)*transform.rotation;
                transform.RotateAround(transform.position - new Vector3(-1, 0.5f, 0), Vector3.forward, -90f);
                state = State.Vertical;
            }
            else if (state == State.Vertical)
            {
                transform.RotateAround(transform.position - new Vector3(-0.5f, 1, 0), Vector3.forward, -90f);
                state = State.Horizontal;
            }
            else {
                transform.RotateAround(transform.position - new Vector3(-0.5f, 0.5f, 0), Vector3.forward, -90f);
            }

        }


        //前翻
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {


            if (state == State.Horizontal)
            {
                //transform.rotation = Quaternion.AngleAxis(90f, transform.forward)*transform.rotation;
                transform.RotateAround(transform.position - new Vector3(0, 0.5f, -0.5f), Vector3.right, 90f);

            }
            else if (state == State.Vertical)
            {
                transform.RotateAround(transform.position - new Vector3(0f, 1, -0.5f), Vector3.right, 90f);
                state = State.HV;
            }
            else {
                transform.RotateAround(transform.position - new Vector3(0f, 0.5f, -1f), Vector3.right, 90f);
                state = State.Vertical;
            }

        }

        //后翻
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {


            if (state == State.Horizontal)
            {
                //transform.rotation = Quaternion.AngleAxis(90f, transform.forward)*transform.rotation;
                transform.RotateAround(transform.position - new Vector3(0, 0.5f, 0.5f), Vector3.right, -90f);

            }
            else if (state == State.Vertical)
            {
                transform.RotateAround(transform.position - new Vector3(0f, 1, 0.5f), Vector3.right, -90f);
                state = State.HV;
            }
            else
            {
                transform.RotateAround(transform.position - new Vector3(0f, 0.5f, 1f), Vector3.right, -90f);
                state = State.Vertical;
            }

        }
    }




    void OnTriggerEnter(Collider coll)
    {
        if (coll.gameObject.tag == "Empty")
        {
            Vector3 fx = (new Vector3(coll.transform.position.x, 0, coll.transform.position.z) - new Vector3(transform.position.x, 0, transform.position.z)).normalized;
            //transform.Translate(fx * 0.1f);
            GetComponent<Rigidbody>().AddForce(fx * 20);
            Debug.Log("掉下去");
            
        } else if(coll.gameObject.tag=="EndPoint"&&state==State.Vertical&&Mathf.Abs(coll.transform.position.x-transform.position.x)<=.1f&&Mathf.Abs(coll.transform.position.z-transform.position.z)<=.1f){
            GetComponent<BoxCollider>().isTrigger = true;
            StartCoroutine(waitToLoadScene());
            
        }
       
    }

    IEnumerator waitToLoadScene() {
        yield return new WaitForSeconds(1f);
        SceneManager.LoadScene(Setting.sceneIndex + 1);
    }

    void OnCollisionEnter(Collision coll)
    {
        //if (coll.gameObject.tag == "Empty")
        //{
        //    Vector3 fx = (coll.transform.position - transform.position).normalized;
        //    transform.Translate(fx * 0.1f);
        //    Debug.Log("掉下去");
        //}
        


    }
}
